<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="/static/layui/css/layui.css"/>
    <!-- 引入 layui.js -->
    <script src="/static/layui/layui.js"></script>
</head>
<body>
<form class="layui-form" style="padding: 64px">
    <div class="layui-form-item">
        <label class="layui-form-label">账号</label>
        <div class="layui-input-block">
            <input type="text" name="mobile" required lay-verify="title" autocomplete="off" placeholder="请输入账号"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="password" name="password" required lay-verify="pass" autocomplete="off" placeholder="请输入密码"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">验证码</label>
        <div class="layui-input-block">
            <input type="text" name="captcha" required lay-verify="required" autocomplete="off" placeholder="请输入验证码"
                   class="layui-input">
            <img src="{:captcha_src()}" alt="captcha"
                 onclick="this.src='{:captcha_src()}?' + Math.random();"
                 style="position: absolute;top: 1px;right: 1px;"/>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script>
    layui.use(function () {
        let form = layui.form;
        let layer = layui.layer;
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            }, pass: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ], content: function (value) {
                layedit.sync(editIndex);
            }
        });
        form.on('submit(demo1)', function (data) {
            if (data.field.mobile.length < 11) {
                layer.msg('手机号码格式不正确');
                return false;
            }
            if (data.field.password.length < 6) {
                layer.msg('密码长度不能低于6位');
                return false;
            }
            if (data.field.captcha.length < 4) {
                layer.msg('验证码长度不能低于4位');
                return false;
            }
            layui.jquery.ajax({
                url: '/index/index/login/',
                data: data.field,
                type: "POST",
                success: function (data) {
                    if (data.code == 2) {
                        layui.layer.msg(data.msg);
                        return false;
                    }
                    location.reload();
                    return false;
                }
            })
            return false;
        });
    });
</script>
</body>
</html>
